package com.itboye.core.object;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.itboye.baidu.util.NetWorkActivity;
import com.itboye.baidu.util.TipsToast;
import com.itboye.core.alert.ByAlert;
import com.itboye.core.alert.LoadingDialog;
import com.itboye.jmsfhz.R;
import com.itboye.jmsfhz.app.MainApplication;
/**
 * 继承自该Fragment的类不需要findViewById，在onActivityCreated中调用super.onActivityCreated()后即可直接使用全局的控件了
 * 
 * @author Mr.w
 *
 */
public abstract class BaseFragment extends Fragment {
	private static TipsToast tipsToast;
	LoadingDialog progressDialog = null;
	public static final String EVENT_TYPE_UNKNOWN = BaseActivity.class.getName()+"_unknown";
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		smartInject(getView());
		progressDialog = new LoadingDialog();
		onMyActivityCreated();
	}

 
	@SuppressWarnings("unchecked")
	protected void smartInject(View view) {  
        try {
			Class<? extends Fragment> clz= getClass();   
			while ( clz!=BaseFragment.class) {
				 Field[] fs=clz.getDeclaredFields();  
			     Resources res = getResources();  
			     String packageName = getActivity().getPackageName();  
			     for (Field field : fs) {  
			     	if (!View.class.isAssignableFrom(field.getType())) {
						continue;
					}
			         int viewId = res.getIdentifier(field.getName(), "id", packageName);  
			         if (viewId==0)   
			             continue;  
			         field.setAccessible(true);  
			         try {  
			             View v=view.findViewById(viewId);  
			             field.set(this,v);  
			             Class<?> c=field.getType();  
			             Method m=c.getMethod("setOnClickListener", android.view.View.OnClickListener.class);  
			             m.invoke(v, this);  
			         } catch (Throwable e) {  
			         }  
			         field.setAccessible(false);  
			           
			     }  
			       
			     clz=(Class<? extends Fragment>) clz.getSuperclass();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    } 
	
	
	public static void showTips(int iconResId, String tips) {
		// TODO Auto-generated method stub
		if (tipsToast != null) {
			if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
				tipsToast.cancel();
			}
		} else {
			tipsToast = TipsToast.makeText(MainApplication.context, tips,
					TipsToast.LENGTH_SHORT);
		}
		tipsToast.show();
		tipsToast.setIcon(iconResId);
		tipsToast.setText(tips);
	}
	/**
	 * @param data
	 * @return 
	 * 2016-4-23 hebidu
	 */
	/**
	 * @param data
	 * @return 2016-4-23 hebidu
	 */
	public ResultEntity handlerError(Object data) {
		ResultEntity result = (ResultEntity) data;
		if (result == null) {
			result = new ResultEntity("-1", "数据格式错误!~", data);
			return result;
		}

		if (result.hasError()) {

			if (result.getMsg().contains("java.net.UnknownHostException")) {
				ByAlert.alert("请连接网络 ");
			} else {
				ByAlert.alert("请连接网络 ");
			}
			result.setEventType(EVENT_TYPE_UNKNOWN);
			return result;
		}

		return result;
	}
	

	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(initView(), null);
	}
	public abstract int initView();
	public abstract void onMyActivityCreated();
	/**
	 * 显示进度对话框,带有消息 flag 是否可以取消
	 */
	@SuppressLint("NewApi")
	public void showProgressDialog(String message, boolean flag) {
		if (progressDialog == null)
			return;
		setProgressDialogMessage(message);

		progressDialog.show(getActivity().getFragmentManager(), message);
		progressDialog.setCancelable(flag);

	}

	/**
	 * 设置进度对话框消息
	 * 
	 * @param message
	 */
	public void setProgressDialogMessage(String message) {
		progressDialog.setMsg(message);
	}

	/**
	 * 关闭进度对话框
	 */
	@SuppressLint("NewApi")
	public void closeProgressDialog() {
		if (progressDialog != null) {
			if (progressDialog.getDialog().isShowing()) {
				progressDialog.dismiss();
			}
		}
	}
}
